
package com.yimixian.app.util;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.widget.ProgressBar;

import com.yimixian.app.ui.ProgressDialogFragment;
import com.yimixian.app.ui.dialog.ProgressDialog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import de.greenrobot.event.EventBus;

/**
 * Created by songzy on 2015/11/26.
 */
public class CacheUtils {

//    public static ProgressBar mProgressBar;

    public static void clearCache(File file, Context context) {
//        ProgressDialog progressDialog = new ProgressDialog();
//        Dialog dialog = progressDialog.getProgressDialog(context);
//        mProgressBar = progressDialog.getProgressBar();
//        try {
//            mProgressBar.setMax(100);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (dialog != null) {
//            dialog.show();
//        }
//        long size = 0;
//        long totalSize = 0;
//        try {
//            totalSize = getFileSizes(file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        ProgressDialogFragment progressDialogFragment = UiUtils.showProgressDialog(context, "清理中");
        if (file.exists()) {
                for (File file1 : file.listFiles()) {
//                    size += getFileSize(file1);
//                    int progress = (int) ((float) size / totalSize * 100);
//                    mProgressBar.setProgress(progress);
                    file1.delete();
                }
//                if (dialog != null && dialog.isShowing()) {
//                    dialog.dismiss();
//                }
                if(progressDialogFragment != null) {
                    progressDialogFragment.dismiss();
                }
                EventBus.getDefault().post(new String("clear_finish"));
                Log.e("--------", android.text.format.Formatter.formatFileSize(context,
                        getFileSizes(file)));
        } else {
//            if (dialog != null && dialog.isShowing()) {
//                dialog.dismiss();
//            }
            if(progressDialogFragment != null) {
                progressDialogFragment.dismiss();
            }
            Log.e("--------", "null");
        }
    }

    public static long getFileSizes(File file){
        long size = 0;
        if (file.exists()) {
            File flist[] = file.listFiles();
            for (int i = 0; i < flist.length; i++) {
                if (flist[i].isDirectory()) {
                    size = size + getFileSizes(flist[i]);
                } else {
                    size = size + getFileSize(flist[i]);
                }
            }
        }
        return size;
    }

    private static long getFileSize(File file){
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                size = fis.available();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return size;
    }
}
